Page({
  data: {
    habits: [
      {
        id: 1,
        name: '晨跑',
        description: '每天早上跑步5公里',
        streak: 5,
        checkedToday: false,
        icon: 'run'
      },
      {
        id: 2,
        name: '阅读',
        description: '每天阅读1小时',
        streak: 3,
        checkedToday: true,
        icon: 'book'
      },
      {
        id: 3,
        name: '冥想',
        description: '每天冥想15分钟',
        streak: 10,
        checkedToday: false,
        icon: 'meditation'
      }
    ],
    showAddDialog: false,
    editHabitIndex: -1,
    habitForm: {
      name: '',
      description: '',
      icon: 'star'
    },
    iconOptions: [
      'star', 'run', 'book', 'meditation', 'water', 'sleep', 
      'gym', 'study', 'eat', 'code', 'music', 'write', 'plan'
    ],
    showCheckAnimation: false,
    animatingHabitId: null
  },
  
  showAddHabitDialog() {
    this.setData({
      showAddDialog: true,
      editHabitIndex: -1,
      habitForm: {
        name: '',
        description: '',
        icon: 'star'
      }
    });
  },
  
  closeDialog() {
    this.setData({
      showAddDialog: false
    });
  },
  
  saveHabit() {
    const form = this.data.habitForm;
    if (!form.name) {
      wx.showToast({
        title: '请填写习惯名称',
        icon: 'none'
      });
      return;
    }
    
    if (this.data.editHabitIndex >= 0) {
      const habits = this.data.habits;
      habits[this.data.editHabitIndex].name = form.name;
      habits[this.data.editHabitIndex].description = form.description;
      habits[this.data.editHabitIndex].icon = form.icon;
      
      this.setData({ habits });
      wx.showToast({
        title: '修改成功',
        icon: 'success'
      });
    } else {
      const habits = this.data.habits;
      habits.push({
        id: habits.length + 1,
        name: form.name,
        description: form.description,
        icon: form.icon,
        streak: 0,
        checkedToday: false
      });
      
      this.setData({ habits });
      wx.showToast({
        title: '添加成功',
        icon: 'success'
      });
    }
    
    this.closeDialog();
  },
  
  onInputChange(e) {
    const field = e.currentTarget.dataset.field;
    const value = e.detail.value;
    this.setData({
      [`habitForm.${field}`]: value
    });
  },
  
  selectIcon(e) {
    const icon = e.currentTarget.dataset.icon;
    this.setData({
      'habitForm.icon': icon
    });
  },
  
  editHabit(e) {
    const index = e.currentTarget.dataset.id;
    const habit = this.data.habits[index];
    
    this.setData({
      showAddDialog: true,
      editHabitIndex: index,
      habitForm: {
        name: habit.name,
        description: habit.description,
        icon: habit.icon || 'star'
      }
    });
  },
  
  deleteHabit(e) {
    const index = e.currentTarget.dataset.id;
    
    wx.showModal({
      title: '提示',
      content: '确定要删除这个习惯吗？',
      success: res => {
        if (res.confirm) {
          const habits = this.data.habits;
          habits.splice(index, 1);
          this.setData({ habits });
          wx.showToast({
            title: '删除成功',
            icon: 'success'
          });
        }
      }
    });
  },
  
  checkHabit(e) {
    const index = e.currentTarget.dataset.id;
    let habits = this.data.habits;
    
    if (!habits[index].checkedToday) {
      this.setData({
        showCheckAnimation: true,
        animatingHabitId: habits[index].id
      });
      
      setTimeout(() => {
        habits[index].checkedToday = true;
        habits[index].streak += 1;
        
        this.setData({ 
          habits,
          showCheckAnimation: false,
          animatingHabitId: null
        });
        
        wx.showToast({
          title: '打卡成功',
          icon: 'success'
        });
      }, 1000);
    }
  },
  
  getIconPath(icon) {
    return `/assets/icons/${icon}.png`;
  }
}) 